/*
 * locTopics : id, title, updateTime, user_name, postCount
 * posts : id, content, updateTime, user_id, user_name
 */

var MapLoc_TopicCtrl = dojo.declare(null, {
	//constructor/destructor
	constructor : mapLoc_topicCtrl_constructor,
	destroy : mapLoc_topicCtrl_destructor,
	
	//methods
	addPost : mapLoc_topicCtrl_addPost,
	init : mapLoc_topicCtrl_init,
	postReply : mapLoc_topicCtrl_postReply
}); 

function mapLoc_topicCtrl_constructor(){
	var obj = this;
	
	obj._postList = null;
	obj._curTopic = -1;
	obj._replyUserId = -1;
	obj._replyUserName = "";
}

function mapLoc_topicCtrl_destructor(){
}

function mapLoc_topicCtrl_addPost(){
	var obj = this;
	$("#add_post #button_add_post").unbind();
	function showMsg(item, msg, type){
		$.validationEngine.buildPrompt(item, msg, type);
		setTimeout("$.validationEngine.closePrompt('" + item + "');", 2000);
	}
	
	if ($("#post_bound #input_content").val().length == 0){
		showMsg("#post_bound #input_content", "回复内容不能为空。", "error");
		$("#add_post #button_add_post").click(function(){obj.addPost();});
		return;
	}
	if ($("#post_bound #input_content").val().length > 2048){
		showMsg("#post_bound #input_content", "回复内容太长。", "error");
		$("#add_post #button_add_post").click(function(){obj.addPost();});
		return;
	}
	showMsg("#post_bound #button_add_post", "正在发送，请稍候...", "load");
	
	var content = trimContent($("#post_bound #input_content").val());
	$.post("/map/addSchoolMapLocTopicPost", {id : obj._curTopic, content : content, reply_user_id : obj._replyUserId}, function(data){
		if (data == "success"){
			$("#post_bound #input_content").val("");
			obj._replyUserId = -1;
			obj._postList.update();
			$("#add_post #button_add_post").click(function(){obj.addPost();});
			showMsg("#post_bound #button_add_post", "回复发起成功！", "pass");
		}  else if (data == "too fast"){
			$("#add_post #button_add_post").click(function(){obj.addPost();});
			showMsg("#post_bound #button_add_post", "对不起，您发起回复的速度太快了，请稍候再试。", "error");
		}  else {
			$("#add_post #button_add_post").click(function(){obj.addPost();});
			showMsg("#post_bound #button_add_post", "回复发送失败。", "error");
		}  
		$("#input_content").removeAttr("disabled");
	}, "json");
}

function mapLoc_topicCtrl_init(){
	var obj = this;
	obj._curTopic = $(".page_variable div[var_name='info']").attr("id");
	obj._postList = new TabList();
	obj._postList.init({baseItem : $("#postlist"), listName : "回复", defaultTab : 0, 
		tabHidden : true,
		emptyHtml : ["<div style='padding-left:10px;'>暂无回复。</div>"],
		tabs : [["", "/map/getSchoolMapLocTopicPostsByTopicId", {"id" : obj._curTopic}, "/map/getSchoolMapLocTopicPostCountByTopicId", {"id" : obj._curTopic}]],
		itemHtml : [$("#postlist_item_example").html()],
		itemHtmlMapping : [[[1, "post_content", "text"], [2, "post_date", "text"], [3, "post_user_id", "text"], [4, "post_user", "text"], [obj.postReply, "post_reply_link", "link"]]]
	});
	
	$("#add_post #button_add_post").unbind();
	$("#add_post #button_add_post").click(function(){obj.addPost();});
}

function mapLoc_topicCtrl_postReply(item){
	var obj = mapLoc_topicCtrl;
	var content = $("#post_bound #input_content").val();
	var start = 0;
	while (start < content.length && content[start] == " ") start ++;
	content = content.substr(start);
	if (obj._replyUserId != -1){
		var replyStr = "回复" + obj._replyUserName + "：";
		if (content.substr(0, replyStr.length) == replyStr) content = content.substr(replyStr.length);
	}
	obj._replyUserId = parseInt(item.parent().find("#post_user_id").html());
	obj._replyUserName = item.parent().find("#post_user").html();
	content = "回复" + obj._replyUserName + "：" + content;
	$("#post_bound #input_content").val(content);
}

if (dojo.getObject("mapLoc_topicCtrl") == null) var mapLoc_topicCtrl = new MapLoc_TopicCtrl;
$(document).ready(function(){
	mapLoc_topicCtrl.init();
});
